package ro.sergiu.finder.models.grid;

import java.awt.Component;
import ro.sergiu.finder.models.property.PropertyEditorGenerator;
import ro.sergiu.finder.models.property.AutoresizeMode;
import ro.sergiu.finder.models.property.SelectionMode;

public class GridModelPropertyEditor extends PropertyEditorGenerator {

    private GridModelPanel editor = null;

    public GridModelPropertyEditor() {
        GridModel defaultValue = getDefaultValue();
        editor = new GridModelPanel(defaultValue);
        setValue(defaultValue);
    }

    @Override
    public Component getCustomEditor() {
        return editor;
    }

    @Override
    public Object getValue() {
        GridModel ret = new GridModel();
        ret.setAutoresizeMode((AutoresizeMode) editor.resizeMode.getSelectedItem());
        ret.setSelectionMode((SelectionMode) editor.selectionMode.getSelectedItem());
        ret.setSortableRecords(editor.sortableRecords.isSelected());
        ret.setMovableColumns(editor.movableColumns.isSelected());
        ret.setOddForeground(editor.oddColor.getForeground());
        ret.setOddBackground(editor.oddColor.getBackground());
        ret.setEvenForeground(editor.evenColor.getForeground());
        ret.setEvenBackground(editor.evenColor.getBackground());
        return ret;
    }

    @Override
    public final void setValue(Object bean) {
        if (bean == null) {
            bean = getDefaultValue();
        }
        GridModel tableCustom = (GridModel) bean;
        editor.resizeMode.setSelectedItem(tableCustom.getAutoresizeMode());
        editor.selectionMode.setSelectedItem(tableCustom.getSelectionMode());
        editor.sortableRecords.setSelected(tableCustom.isSortableRecords());
        editor.movableColumns.setSelected(tableCustom.isMovableColumns());
        editor.oddColor.setForeground(tableCustom.getOddForeground());
        editor.oddColor.setBackground(tableCustom.getOddBackground());
        editor.evenColor.setForeground(tableCustom.getEvenForeground());
        editor.evenColor.setBackground(tableCustom.getEvenBackground());
        super.setValue(bean);
    }

    @Override
    public final GridModel getDefaultValue() {
        return new GridModel();
    }
}
